// subpage/pages/order/order.js
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailUserInfo:'',
    orderlist:'',
    index: 0,
    multiIndex: [0, 0, 0],
    startTime:'',
    endTime:'',
    isExistsOrder:false,
    chargeOrder:[
     
    ],
    date: '2021',
    time:'12:30',
    show: false,
    isShowFirst: 0,
    active: true,
    orderState:[
      {'id':99,'label':'全部'},
      {'id':0,'label':'待处理'},
      {'id':1,'label':'维修中'},
      {'id':3,'label':'已完成'},
    ],
    currentNav: 0,
    isRefresh: false,
    totalPage: 0,
    page: 1,
    isShowBottom: false,
    hasNext:false,
    isRequest:true,
    ordertype:false,
    islogin:true,
    showStart: false,
    startRe: false,
    startDate: "",
    startTime: '12:30',
    oid: ''
  },
  pageChange(val){
    console.log('组件传过来的值', val)
    this.setData({
      page: val.detail
    })
    this.getOrderList()
  },
  preventTouchMove(){
    return false
  },
  onshow(e){
    this.setData({ showStart: true , oid:  e.target.dataset.id, key: e.target.dataset.key});
  },
  onstartRepair(e){
    console.log('开始维修')
    this.setData({ startRe: true , oid:  e.target.dataset.id, key: e.target.dataset.key});
  },
  onCloseStart(){
    this.setData({ startRe: false, date: this.data.startDate, time: this.data.startTime });
  },
  onClose() {
    this.setData({ showStart: false, date: this.data.startDate, time: this.data.startTime });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },

    //选择时间
    bindDateChange(e){
      console.log('e', e)
      this.setData({
        date: e.detail.value
      })
    },
    bindTimeChange(e){
      this.setData({
        time: e.detail.value
      })
    },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      show: false,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
  },
  callPhone(e){
    wx.makePhoneCall({
      phoneNumber: '1340000' //仅为示例，并非真实的电话号码
    })
  },
  navi_btn(e){
    let lng = e.target.dataset.lng
    let lat = e.target.dataset.lat
    let address = e.target.dataset.address
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        wx.openLocation({
          latitude:Number(lat),
          longitude: Number(lng),
          name: address,
          scale: 16, //缩放比例为18
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   isShowFirst : options.state
    // })
    let year = new Date().getFullYear()
    let month = new Date().getMonth() +1
    let day = new Date().getDate()
    month = month > 9 ? ""+month : "0"+month
    day = day >9 ? ""+day : "0"+day
    let date = year +"-"+ month+'-'+ day
    this.setData({
      date : date,
      startDate: date
    })
  },
    //开始w维修
    startRepair(e){
      console.log('e', e)
      let that = this
      let time = this.data.date +' '+ this.data.time+ ':00'
      let sear = {id: this.data.oid, orderRepairTime: time, state: 1}
      let tag = "chargeOrder["+this.data.key+"].state"
      // that.setData({
      //   [tag]: 1
      // })
      service("POST","/devops/repair/handle",sear).then(res => {
        wx.showToast({ 
          title: res.data.msg,
          icon: 'none'
        })
         if(res.data.code ==0){
          that.setData({
            [tag]: 1
          })
        }
      })
    },
    //维修完成
    finish(e){
      console.log('e', e)
      let that = this
      let time = this.data.date + ' '+ this.data.time
      let sear = {id: this.data.oid, repairTime: time, state: 2}
      let tag = "chargeOrder["+this.data.key+"].state"
      // that.setData({
      //   [tag]: 2
      // })
      service("POST","/devops/repair/complete",sear).then(res => {
        wx.showToast({ 
          title: res.data.msg,
          icon: 'none'
        })
         if(res.data.code ==0){
          that.setData({
            [tag]: 2
          })
        }
      })
    },
  goLogin(){
    wx.navigateTo({
      url: '/subpage/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // let order = this.selectComponent("#orderlist")
    // this.setData({
    //   orderlist: order
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('token')){
      this.setData({
        islogin: true,
        isRequest: true,
        chargeOrder: [],
        page:1
      })
      this.getOrderList()
    }else{
      this.setData({
        islogin: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  //自定义上拉加载
  loadMore(type) {
    return
      //如果总页数 小于等于 当前页数 提示无更多数据
      if(!this.data.isRefresh){
        console.log('加载中')
        if (this.data.hasNext) {
          this.setData({
            page: this.data.page + 1,
            isRefresh:true
          })
          this.selectComponent("#orderlist").noMore('loading')
          this.getOrderList(2)
        } else{
          console.log('没有更多')
          this.selectComponent("#orderlist").noMore('nomore')
          console.log('没有更多')
          // this.setData({
          //   isShowBottom: true
          // })
        }
      }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    return
    console.log("下拉刷新")
    if (!this.data.isRefresh) {
      //如果总页数 小于等于 当前页数 提示无更多数据
      if (this.data.hasNext) {
        this.setData({
          page: this.data.page + 1,
          isRefresh:true
        })
        this.getOrderList(2)
      } else{
        console.log('没有更多1')
        this.setData({
          isShowBottom: true
        })
      }
    }
  },
  /* 切换显示内容 */
  changeShowType(e){
    this.setData({
      isShowFirst: e.target.dataset.id,
      // page: 1
    });
    // this.getOrderList()
  },
  /* 手动滑动swiper触发的事件 */
  // onChange(e){
  //   console.log(e)
  //   this.setData({
  //     isShowFirst: e.detail.current+1
  //   })
  // },
//自定义下拉刷新
refresh(e){
  console.log('父组件刷新',e)
  this.setData({
    page: 1,
    
  })
  this.getOrderList(1)
},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeTab(e) {
   console.log('eeeeeeeeeeeeee',e)
   this.setData({
    isShowFirst: e.target.dataset.id,
    chargeOrder: [],
     page:1
  })
  this.getOrderList()
  },
  orderDetail(e) {
    console.log('传输',e)
    wx.navigateTo({
      url: '/devops/pages/repair/info?id='+e.currentTarget.dataset.id,
    })
    
  },
  goRepair(){
    wx.redirectTo({
      url: '/devops/pages/repair/repair',
    })
  },
  goOrder(){
    wx.redirectTo({
      url: '/devops/pages/order/order',
    })
  },
  goPower(){
    wx.redirectTo({
      url: '/devops/pages/power/power',
    })
  },
  goPile(){
    wx.redirectTo({
      url: '/devops/pages/pile/index',
    })
  },
  getOrderList(type) {
    console.log('刷新形式',type)
    let that = this
    if(that.data.isRequest === false){
      return
    }
    this.setData({
      isRequest: false
    })
    let sear
    if(this.data.isShowFirst == 99){
      sear = {page: this.data.page}
    }else {
      sear = {state: this.data.isShowFirst,page: this.data.page}
    }
    service("POST", "/devops/repair/list", sear).then(res => {
      if(res.data.code == 414){
        this.setData({
          islogin: false
        })
        wx.setStorageSync('token', '')
        return
      }
      that.setData({
        isRefresh: false
      })
      // if(type==1){
      //   that.setData({
      //     chargeOrder: []
      //   })
      // }
      if(res.data.code ==0){
        let data = res.data.page.list
        data.map(item => {
          item.minintus = 0
          if(item.beginTime){
            if(item.endTime){
             item.minintus = Math.abs(new Date(item.endTime) -new Date(item.beginTime))/1000/60
            }else{
              item.minintus =Math.abs(new Date() -new Date(item.beginTime))/1000/60
            }
          }
          return item
        })
        that.setData({
          chargeOrder: res.data.page.list,
          isRefresh: false,
          hasNext:res.data.page.hasNext,
          isRequest:true
        })
      }
      // if(type == 1) {
      //   that.selectComponent("#orderlist").reSet()
      // }
    })
  },
  //复制
  copy: function (e) {
    console.log('复制',e)
    wx.setClipboardData({
      data: e.target.dataset.oid,
    });
    // wx.showToast({
    //   title: '复制成功',
    // })
  }
})